set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the Trace file
set file1 [open trace.tr w]
$ns trace-all $file1

#Open the NAM trace file
set file2 [open trace.nam w]
$ns namtrace-all $file2

#Define a 'finish' procedure
proc finish {} {
        global ns file1 file2
        $ns flush-trace
        close $file1
        close $file2
        exec nam trace.nam &
        exit 0
}

#Next line should be commented out to have the static routing
$ns rtproto DV

#Creat network nodes
#n0 represents the Internet
set n(0) [$ns node]
$n(0) shape "hexagon"

#create router nodes
set numRouters 20
for {set i 1} {$i <= $numRouters} {incr i} {
    set n($i) [$ns node]
}

#create end nodes
set numEnds 75
for {set i 21} {$i <= $numEnds} {incr i} {
    set n($i) [$ns node]
    $n($i) shape "box"
}


#Create duplex links between the nodes
$ns duplex-link $n(0) $n(1)  44.74Mb  10ms DropTail
$ns duplex-link $n(0) $n(2)  44.74Mb  10ms DropTail
$ns duplex-link $n(0) $n(10) 44.74Mb  10ms DropTail
$ns duplex-link $n(0) $n(11) 44.74Mb  10ms DropTail
$ns duplex-link $n(0) $n(20) 44.744Mb 10ms DropTail

$ns duplex-link $n(1) $n(3) 100Mb 10ms DropTail
$ns duplex-link $n(1) $n(4) 100Mb 10ms DropTail
$ns duplex-link $n(2) $n(3) 100Mb 10ms DropTail
$ns duplex-link $n(2) $n(4) 100Mb 10ms DropTail

$ns duplex-link $n(4) $n(5) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(6) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(7) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(8) 100Mb 10ms DropTail
$ns duplex-link $n(4) $n(9) 100Mb 10ms DropTail

$ns duplex-link $n(11) $n(12) 100Mb 10ms DropTail
$ns duplex-link $n(11) $n(13) 100Mb 10ms DropTail
$ns duplex-link $n(11) $n(14) 100Mb 10ms DropTail

$ns duplex-link $n(14) $n(15) 100Mb 10ms DropTail
$ns duplex-link $n(14) $n(16) 100Mb 10ms DropTail
$ns duplex-link $n(14) $n(17) 100Mb 10ms DropTail
$ns duplex-link $n(14) $n(18) 100Mb 10ms DropTail
$ns duplex-link $n(14) $n(19) 100Mb 10ms DropTail


$ns duplex-link $n(3) $n(21) 100Mb 10ms DropTail
$ns duplex-link $n(3) $n(22) 100Mb 10ms DropTail
$ns duplex-link $n(3) $n(23) 100Mb 10ms DropTail
$ns duplex-link $n(3) $n(24) 100Mb 10ms DropTail

$ns duplex-link $n(5) $n(25) 100Mb 10ms DropTail
$ns duplex-link $n(5) $n(26) 100Mb 10ms DropTail
$ns duplex-link $n(5) $n(27) 100Mb 10ms DropTail

$ns duplex-link $n(6) $n(28) 100Mb 10ms DropTail
$ns duplex-link $n(6) $n(29) 100Mb 10ms DropTail
$ns duplex-link $n(6) $n(30) 100Mb 10ms DropTail
$ns duplex-link $n(6) $n(31) 100Mb 10ms DropTail

$ns duplex-link $n(7) $n(32) 100Mb 10ms DropTail
$ns duplex-link $n(7) $n(33) 100Mb 10ms DropTail
$ns duplex-link $n(7) $n(34) 100Mb 10ms DropTail
$ns duplex-link $n(7) $n(35) 100Mb 10ms DropTail

$ns duplex-link $n(8) $n(36) 100Mb 10ms DropTail
$ns duplex-link $n(8) $n(37) 100Mb 10ms DropTail
$ns duplex-link $n(8) $n(38) 100Mb 10ms DropTail
$ns duplex-link $n(8) $n(39) 100Mb 10ms DropTail

$ns duplex-link $n(9) $n(40) 100Mb 10ms DropTail
$ns duplex-link $n(9) $n(41) 100Mb 10ms DropTail
$ns duplex-link $n(9) $n(42) 100Mb 10ms DropTail
$ns duplex-link $n(9) $n(43) 100Mb 10ms DropTail

$ns duplex-link $n(12) $n(44) 100Mb 10ms DropTail
$ns duplex-link $n(12) $n(45) 100Mb 10ms DropTail
$ns duplex-link $n(12) $n(46) 100Mb 10ms DropTail

$ns duplex-link $n(13) $n(47) 100Mb 10ms DropTail
$ns duplex-link $n(13) $n(48) 100Mb 10ms DropTail
$ns duplex-link $n(13) $n(49) 100Mb 10ms DropTail
$ns duplex-link $n(13) $n(50) 100Mb 10ms DropTail

$ns duplex-link $n(15) $n(51) 100Mb 10ms DropTail
$ns duplex-link $n(15) $n(52) 100Mb 10ms DropTail
$ns duplex-link $n(15) $n(53) 100Mb 10ms DropTail

$ns duplex-link $n(16) $n(54) 100Mb 10ms DropTail
$ns duplex-link $n(16) $n(55) 100Mb 10ms DropTail
$ns duplex-link $n(16) $n(56) 100Mb 10ms DropTail
$ns duplex-link $n(16) $n(57) 100Mb 10ms DropTail

$ns duplex-link $n(17) $n(58) 100Mb 10ms DropTail
$ns duplex-link $n(17) $n(59) 100Mb 10ms DropTail
$ns duplex-link $n(17) $n(60) 100Mb 10ms DropTail
$ns duplex-link $n(17) $n(61) 100Mb 10ms DropTail

$ns duplex-link $n(18) $n(62) 100Mb 10ms DropTail
$ns duplex-link $n(18) $n(63) 100Mb 10ms DropTail
$ns duplex-link $n(18) $n(64) 100Mb 10ms DropTail
$ns duplex-link $n(18) $n(65) 100Mb 10ms DropTail

$ns duplex-link $n(19) $n(66) 100Mb 10ms DropTail
$ns duplex-link $n(19) $n(67) 100Mb 10ms DropTail
$ns duplex-link $n(19) $n(68) 100Mb 10ms DropTail
$ns duplex-link $n(19) $n(69) 100Mb 10ms DropTail

$ns duplex-link $n(10) $n(70) 100Mb 10ms DropTail
$ns duplex-link $n(10) $n(71) 100Mb 10ms DropTail
$ns duplex-link $n(10) $n(72) 100Mb 10ms DropTail

$ns duplex-link $n(20) $n(73) 100Mb 10ms DropTail
$ns duplex-link $n(20) $n(74) 100Mb 10ms DropTail
$ns duplex-link $n(20) $n(75) 100Mb 10ms DropTail

$ns cost $n(0) $n(1) 3
$ns cost $n(0) $n(2) 2

#Setup TCP connections for normal traffic
set srcs 20
for {set i 0} {$i < $srcs} {incr i} {
    set src($i) [new Agent/TCP]
    # set network layer (IP) flow ID, to give them blue color
    $src($i) set fid_ 1
}

set sinks 10
for {set i 0} {$i < $sinks} {incr i} {
    set sink($i) [new Agent/TCPSink]
    $sink($i) listen;
}

$n(26) attach $src(0) 7771
$n(29) attach $src(1) 7771
$n(30) attach $src(2) 7771
$n(33) attach $src(3) 7771
$n(34) attach $src(4) 7771
$n(37) attach $src(5) 7771
$n(39) attach $src(6) 7771
$n(40) attach $src(7) 7771
$n(43) attach $src(8) 7771
$n(46) attach $src(9) 7771
$n(51) attach $src(10) 7771
$n(56) attach $src(11) 7771
$n(57) attach $src(12) 7771
$n(58) attach $src(13) 7771
$n(60) attach $src(14) 7771
$n(64) attach $src(15) 7771
$n(66) attach $src(16) 7771
$n(69) attach $src(17) 7771
$n(70) attach $src(18) 7771
$n(73) attach $src(19) 7771

$n(21) attach $sink(0) 80
$n(22) attach $sink(1) 3306
$n(23) attach $sink(2) 143
$n(24) attach $sink(3) 53
$n(47) attach $sink(4) 80
$n(48) attach $sink(5) 3306
$n(49) attach $sink(6) 143
$n(50) attach $sink(7) 53
$n(72) attach $sink(8) 80
$n(75) attach $sink(9) 80

$ns connect $src(0)  $sink(0)
$ns connect $src(1)  $sink(1)
$ns connect $src(2)  $sink(2)
$ns connect $src(3)  $sink(3)
$ns connect $src(4)  $sink(0)
$ns connect $src(5)  $sink(1)
$ns connect $src(6)  $sink(2)
$ns connect $src(7)  $sink(3)
$ns connect $src(8)  $sink(8)
$ns connect $src(9)  $sink(4)
$ns connect $src(10) $sink(5)
$ns connect $src(11) $sink(6)
$ns connect $src(12) $sink(7)
$ns connect $src(13) $sink(4)
$ns connect $src(14) $sink(5)
$ns connect $src(15) $sink(6)
$ns connect $src(16) $sink(7)
$ns connect $src(17) $sink(9)
$ns connect $src(18) $sink(0)
$ns connect $src(19) $sink(4)

#Setup TCP connections for attacks
set atts 10
for {set i 0} {$i < $atts} {incr i} {
    set hacker($i) [new Agent/TCP]
    # set network layer (IP) flow ID, to give them red color
    $hacker($i) set fid_ 2
}

set victim(0) [new Agent/TCPSink]
$victim(0) listen;

$n(25) attach $hacker(0) 911
$n(25) attach $hacker(1) 911
$n(38) attach $hacker(2) 911
$n(38) attach $hacker(3) 911
$n(44) attach $hacker(4) 911
$n(44) attach $hacker(5) 911
$n(55) attach $hacker(6) 911
$n(55) attach $hacker(7) 911
$n(71) attach $hacker(8) 911
$n(71) attach $hacker(9) 911

$n(38) attach $victim(0) 7777

$ns connect $hacker(0) $victim(0)
$ns connect $hacker(1) $sink(1)
$ns connect $hacker(2) $sink(1)
$ns connect $hacker(3) $sink(2)
$ns connect $hacker(4) $sink(5)
$ns connect $hacker(5) $sink(7)
$ns connect $hacker(6) $sink(6)
$ns connect $hacker(7) $sink(9)
$ns connect $hacker(8) $sink(1)
$ns connect $hacker(9) $sink(9)


#Setup FTP applications to simulate traffic over TCP connections
set ftps 20
for {set i 0} {$i < $ftps} {incr i} {
    set ftp($i) [new Application/FTP]
    $ftp($i) attach-agent $src($i)
}


set ftp_atts 10
for {set i 0} {$i < $ftp_atts} {incr i} {
    set ftp_att($i) [new Application/FTP]
    $ftp_att($i) attach-agent $hacker($i)
}



proc traffic {app start_time stop_time} {
    global ns
    $ns at $start_time "$app start"
    $ns at $stop_time "$app stop"
}

#Start simulation
set ftp_traffics 10
for {set i 0} {$i < $ftp_traffics} {incr i} {
    traffic $ftp($i) 0.1 5.0
}


traffic $ftp_att(0) 1.0 1.5
traffic $ftp_att(1) 1.0 4.5
traffic $ftp_att(2) 2.0 4.5
traffic $ftp_att(3) 2.0 4.5
traffic $ftp_att(4) 1.0 4.5
traffic $ftp_att(5) 1.0 4.5
traffic $ftp_att(6) 1.0 4.5
traffic $ftp_att(7) 1.0 4.5
traffic $ftp_att(8) 0.5 4.5
traffic $ftp_att(9) 0.5 4.5

$ns at 6.0 "finish"

$ns run

